过程式代码(使用数据结构的代码)便于在不该懂既有数据结构的前提下添加新函数,面向对象代码便于在不改动既有函数的前提下添加新类。
过程式代码难以添加新的数据结构,因为必须修改所有的函数;面向对象代码难以添加新的函数,因为必须修改所有类。
过程式代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| public class Square{ public Point topLeft; public double side; } public class Rectangle{ public Point topLeft; public double height; public double width; } public class Circle{ public Point topLeft; public double radius; } public class Geometry{ public double PI = 3.1415926; public double area(Object shape)throws NoSuchShapeException { if(shape instanceof Square){ Square s = (Square)shape; return s.side*s.side; } else if(shape instanceof Rectangle){ Rectangle r = (Rectangle)shape; return r.width*r.height; } else if(shape instanceof Circle){ Circle c = (Circle)shape; return PI*c.radius*c.radius; } throw new NoSuchShapeException(); } }
|
如果此时想在Geometry类中添加一个primeter()函数,这实现起来很简单,现有形状不会因此受到影响:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public class Geometry{ public double PI = 3.1415926; public double area(Object shape)throws NoSuchShapeException { if(shape instanceof Square){ Square s = (Square)shape; return s.side*s.side; } else if(shape instanceof Rectangle){ Rectangle r = (Rectangle)shape; return r.width*r.height; } else if(shape instanceof Circle){ Circle c = (Circle)shape; return PI*c.radius*c.radius; } throw new NoSuchShapeException(); } public double primeter(Object shape)throws NoSuchShapeException { if(shape instanceof Square){ Square s = (Square)shape; return 4*s.side; } else if(shape instanceof Rectangle){ Rectangle r = (Rectangle)shape; return r.width*2+r.height*2; } else if(shape instanceof Circle){ Circle c = (Circle)shape; return 2*PI*c.radius; } throw new NoSuchShapeException(); } }
|
如果此时想添加一个新的形状,那Geometry类中的area()和primeter()都要修改,如果Geometry类中有其他方法的话,那要修改的方法可能会更多。
过程式代码难以添加新的数据结构,因为必须修改所有函数。
面向对象代码示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public class Square implements Shape{ private Point topLeft; private double side; public double area(){ return side*side; } } public class Rectangle implements Shape{ private Point topLeft; private double height; private double width; public double area(){ return height*width; } } public class Circle implements Shape{ private Point topLeft; private double radius; public double PI = 3.1415926; public double area(){ return PI*radius*radius; } } public class Geometry { public double PI = 3.1415926; public double area(Shape shape)throws NoSuchShapeException { shape.area(); } }
|
此时如果你想添加一个新形状,实现起来很简单,现有形状不会因此受到影响:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| public class Square implements Shape{ private Point topLeft; private double side; public double area(){ return side*side; } } public class Rectangle implements Shape{ private Point topLeft; private double height; private double width; public double area(){ return height*width; } } public class Circle implements Shape{ private Point topLeft; private double radius; public double PI = 3.1415926; public double area(){ return PI*radius*radius; } } public class NewShape implements Shape{ private Point topLeft; ...... public double area(){ ...... } }
|
但是如果此时你想添加一个新函数primeter()函数,那就要修改Shape接口,继承了Shape接口的现有形状都需要修改。
面向对象代码难以添加新函数,因为必须修改所有类。